package com.bd03.streaminglearn.mytest

import redis.clients.jedis.Jedis

import scala.io.{BufferedSource, Source}

object WriteDemo {
  //val jedis = JedisPoolDemo.getResource()
  def main(args: Array[String]): Unit = {
    //write2Redis("D:\\data\\ip\\ip.txt")
    //println(binarySearch("1034747723"))
  }

  def binarySearch(ip:String,jedis:Jedis):String={
    var start=0
    var end = 115395
    var province = "weizhi";
    while(start<=end){
      val mid = (start+end)/2
      val str = jedis.hget("ip",mid.toString)
      val value = str.split(",")
      if(ip.toLong>=value(0).toLong&&ip.toLong<=value(1).toLong) {
        province = value(2)
        return province
      }
      if(ip.toLong<value(0).toLong){
        end=mid-1
      }else{
        start=mid+1
      }
    }
    province.toString
  }
  //1.1.0.0|1.1.0.255|16842752|16843007|亚洲|中国|福建|福州||电信|350100|China|CN|119.306239|26.075302
  def write2Redis(path:String,jedis:Jedis): Unit ={
    val path1: BufferedSource = Source.fromFile(path)
    val strings = path1.getLines()
    var i = 0
    strings.foreach(t => {
      val str = t.split("\\|")
      jedis.hset("ip",i.toString,str(2)+","+str(3)+","+str(6))
      i=i+1
    })

  }

}
